﻿#region

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;
using StockholmsStad.Web.UI.Class.Business;
using StockholmsStad.Web.UI.Class.Interfaces;
using StockholmsStad.Web.UI.Class.Model;
using StockholmsStad.Web.UI.Class.Utilities;
using StockholmsStad.Web.UI.UIServiceReference;

#endregion

namespace StockholmsStad.Web.UI.Test
{
    /// <summary>
    ///This is a test class for RegistrationModelTest and is intended
    ///to contain all RegistrationModelTest Unit Tests
    ///</summary>
    [TestClass]
    public class RegistrationModelTest
    {
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        #region Additional test attributes

        #endregion

        /// <summary>
        ///A test for getContactEmail
        ///</summary>
        [TestMethod]
        public void getContactEmailTest()
        {
            var mocks = new MockRepository();
            ISession session = new DictionarySessionAdapter(new Dictionary<string, object>());
            IHttpContextAdapter context = mocks.StrictMock<IHttpContextAdapter>();
            var target = new RegistrationModel(session, mocks.StrictMock<IServiceFacade>());
            string expected = "abc@cd.cd";
            target.Couple.ContactPerson = new ContactPerson
                                              {
                                                  FirstNames = "",
                                                  LastName = "",
                                                  Telephone = "",
                                                  Email = expected,
                                              };
            string actual;
            mocks.ReplayAll();
            actual = target.ContactEmail;
            Assert.AreEqual(expected, actual);
            mocks.VerifyAll();
        }

        /// <summary>
        ///A test for getCouple
        ///</summary>
        [TestMethod]
        public void CoupleTest()
        {
            var mocks = new MockRepository();
            ISession session = new DictionarySessionAdapter(new Dictionary<string, object>());
            IHttpContextAdapter context = mocks.StrictMock<IHttpContextAdapter>();
            var target = new RegistrationModel(session, mocks.StrictMock<IServiceFacade>());
            Couple couple = target.Couple;
            couple.Person1 = new Person("", "hej", null, true, "", true, null);
            Assert.IsNotNull(couple);
            // test continuation of session
            IServiceFacade serviceFacade = mocks.StrictMock<IServiceFacade>();
            mocks.ReplayAll();
            target = new RegistrationModel(session, serviceFacade);
            Couple couple2 = target.Couple;
            Assert.AreEqual(couple.Person1, couple2.Person1);
            Assert.AreEqual(couple, couple2);
            mocks.VerifyAll();
        }

        /// <summary>
        ///A test for Couple
        ///</summary>
        [TestMethod()]
        public void CoupleTest2()
        {
            var mocks = new MockRepository();
            ISession session = new DictionarySessionAdapter(new Dictionary<string, object>());
            IHttpContextAdapter context = mocks.StrictMock<IHttpContextAdapter>();
            var target = new RegistrationModel(session, mocks.StrictMock<IServiceFacade>());
            Couple actual;
            mocks.ReplayAll();
            actual = target.Couple;
            Assert.IsNotNull(actual);
            mocks.VerifyAll();
        }

        /// <summary>
        ///A test for Ceremony
        ///</summary>
        [TestMethod()]
        public void CeremonyTest()
        {
            var mocks = new MockRepository();
            ISession session = new DictionarySessionAdapter(new Dictionary<string, object>());
            IHttpContextAdapter context = mocks.StrictMock<IHttpContextAdapter>();
            var target = new RegistrationModel(session, mocks.StrictMock<IServiceFacade>());
            var expected = new Ceremony();
            Ceremony actual;
            mocks.ReplayAll();
            target.Ceremony = expected;
            actual = target.Ceremony;
            Assert.AreEqual(expected, actual);
            mocks.VerifyAll();
        }

        /// <summary>
        ///A test for getContactEmail
        ///</summary>
        [TestMethod]
        public void getContactEmailTest2()
        {
            var mocks = new MockRepository();
            ISession session = new DictionarySessionAdapter(new Dictionary<string, object>());
            IHttpContextAdapter context = mocks.StrictMock<IHttpContextAdapter>();
            var target = new RegistrationModel(session, mocks.StrictMock<IServiceFacade>());
            string expected = "";
            string actual;
            mocks.ReplayAll();
            actual = target.ContactEmail;
            Assert.AreEqual(expected, actual);
            mocks.VerifyAll();
        }

        /// <summary>
        ///A test for getFullNameFromPerson1
        ///</summary>
        [TestMethod]
        public void getFullNameFromPerson1Test()
        {
            var mocks = new MockRepository();
            ISession session = new DictionarySessionAdapter(new Dictionary<string, object>());
            IHttpContextAdapter context = mocks.StrictMock<IHttpContextAdapter>();
            var target = new RegistrationModel(session, mocks.StrictMock<IServiceFacade>());
            string expected = "Svenne Snorslev";
            target.Couple.Person1 = new Person("Svenne", "Snorslev", null, true, "", true, null);
            string actual;
            mocks.ReplayAll();
            actual = target.FullNameFromPerson1;
            Assert.AreEqual(expected, actual);
            mocks.VerifyAll();
        }

        /// <summary>
        ///A test for getFullNameFromPerson1
        ///</summary>
        [TestMethod]
        public void getFullNameFromPerson1Test2()
        {
            var mocks = new MockRepository();
            ISession session = new DictionarySessionAdapter(new Dictionary<string, object>());
            IHttpContextAdapter context = mocks.StrictMock<IHttpContextAdapter>();
            var target = new RegistrationModel(session, mocks.StrictMock<IServiceFacade>());
            string expected = "";
            string actual;
            mocks.ReplayAll();
            actual = target.FullNameFromPerson1;
            Assert.AreEqual(expected, actual);
            mocks.VerifyAll();
        }

        /// <summary>
        ///A test for RegistrationModel Constructor
        ///</summary>
        [TestMethod]
        public void RegistrationModelConstructorTest()
        {
            var mocks = new MockRepository();
            ISession session = new DictionarySessionAdapter(new Dictionary<string, object>());
            IServiceFacade serviceFacade = mocks.StrictMock<IServiceFacade>();
            IHttpContextAdapter context = mocks.StrictMock<IHttpContextAdapter>();
            mocks.ReplayAll();
            var target = new RegistrationModel(session, serviceFacade);
            Assert.IsNotNull(target);
            mocks.VerifyAll();
        }

        /// <summary>
        ///A test for Person2
        ///</summary>
        [TestMethod]
        public void Person2Test()
        {
            var mocks = new MockRepository();
            ISession session = new DictionarySessionAdapter(new Dictionary<string, object>());
            IHttpContextAdapter context = mocks.StrictMock<IHttpContextAdapter>();
            var target = new RegistrationModel(session, mocks.StrictMock<IServiceFacade>());
            var expected = new Person("", "", null, true, "", true, null);
            mocks.ReplayAll();
            target.Couple.Person2 = expected;
            Person actual = target.Couple.Person2;
            Assert.AreEqual(expected, actual);
            mocks.VerifyAll();
        }

        /// <summary>
        ///A test for Person1
        ///</summary>
        [TestMethod]
        public void Person1Test()
        {
            var mocks = new MockRepository();
            ISession session = new DictionarySessionAdapter(new Dictionary<string, object>());
            IHttpContextAdapter context = mocks.StrictMock<IHttpContextAdapter>();
            var target = new RegistrationModel(session, mocks.StrictMock<IServiceFacade>());
            var expected = new Person("", "", null, true, "", true, null);
            mocks.ReplayAll();
            target.Couple.Person1 = expected;
            Person actual = target.Couple.Person1;
            Assert.AreEqual(expected, actual);
            mocks.VerifyAll();
        }

        /// <summary>
        ///A test for FullNameFromPerson1
        ///</summary>
        [TestMethod]
        public void FullNameFromPerson1Test()
        {
            var mocks = new MockRepository();
            ISession session = new DictionarySessionAdapter(new Dictionary<string, object>());
            IHttpContextAdapter context = mocks.StrictMock<IHttpContextAdapter>();
            var target = new RegistrationModel(session, mocks.StrictMock<IServiceFacade>());
            var person = new Person("Svenne", "Snorslev", null, true, "", true, null);
            target.Couple.Person1 = person;
            string actual;
            mocks.ReplayAll();
            actual = target.FullNameFromPerson1;
            Assert.AreEqual(person.FirstNames + " " + person.LastName, actual);
            mocks.VerifyAll();
        }

        /// <summary>
        ///A test for ContactEmail
        ///</summary>
        [TestMethod]
        public void ContactEmailTest()
        {
            var mocks = new MockRepository();
            ISession session = new DictionarySessionAdapter(new Dictionary<string, object>());
            var facade = mocks.StrictMock<IServiceFacade>();
            IHttpContextAdapter context = mocks.StrictMock<IHttpContextAdapter>();
            var target = new RegistrationModel(session, facade);
            var contact = new ContactPerson()
                              {
                                  Email = "gurka@mupp.nu",
                              };
            target.Couple.ContactPerson = contact;
            string actual;
            mocks.ReplayAll();
            actual = target.ContactEmail;
            Assert.AreEqual(contact.Email, actual);
            mocks.VerifyAll();
        }


        /// <summary>
        ///A test for WeddingCertificateLanguages
        ///</summary>
        [TestMethod]
        public void WeddingCertificateLanguagesTest()
        {
            var mocks = new MockRepository();
            ISession session = new DictionarySessionAdapter(new Dictionary<string, object>());
            var facade = mocks.StrictMock<IServiceFacade>();
            IHttpContextAdapter context = mocks.StrictMock<IHttpContextAdapter>();
            var target = new RegistrationModel(session, facade);
            Expect.Call(facade.WeddingCertificateLanguages).Return(new ValueIdPair[]
                                                                       {
                                                                           new ValueIdPair() {Id = 1, Name = "Swedish"},
                                                                           new ValueIdPair() {Id = 2, Name = "English"},
                                                                       });
            mocks.ReplayAll();
            IEnumerable<string> languages = target.WeddingCertificateLanguages;
            Assert.AreEqual(2, languages.Count());
            Assert.AreEqual("Swedish", languages.First());
            mocks.VerifyAll();
        }

        /// <summary>
        ///A test for CeremonySpecialNeeds
        ///</summary>
        [TestMethod]
        public void CeremonySpecialNeedsTest()
        {
            var mocks = new MockRepository();
            ISession session = new DictionarySessionAdapter(new Dictionary<string, object>());
            var facade = mocks.StrictMock<IServiceFacade>();
            IHttpContextAdapter context = mocks.StrictMock<IHttpContextAdapter>();
            var target = new RegistrationModel(session, facade);
            Expect.Call(facade.CeremonySpecialNeeds).Return(new ValueIdPair[]
                                                                {
                                                                    new ValueIdPair() {Id = 1, Name = "Rullstol"},
                                                                });
            mocks.ReplayAll();
            IEnumerable<string> languages = target.CeremonySpecialNeeds;
            Assert.AreEqual(1, languages.Count());
            Assert.AreEqual("Rullstol", languages.First());
            mocks.VerifyAll();
        }

        /// <summary>
        ///A test for CeremonyLanguages
        ///</summary>
        [TestMethod]
        public void CeremonyLanguagesTest()
        {
            var mocks = new MockRepository();
            ISession session = new DictionarySessionAdapter(new Dictionary<string, object>());
            var facade = mocks.StrictMock<IServiceFacade>();
            IHttpContextAdapter context = mocks.StrictMock<IHttpContextAdapter>();
            var target = new RegistrationModel(session, facade);
            Expect.Call(facade.CeremonyLanguages).Return(new ValueIdPair[]
                                                             {
                                                                 new ValueIdPair() {Id = 1, Name = "Swedish"},
                                                                 new ValueIdPair() {Id = 2, Name = "English"},
                                                             });
            mocks.ReplayAll();
            IEnumerable<string> languages = target.CeremonyLanguages;
            Assert.AreEqual(2, languages.Count());
            Assert.AreEqual("Swedish", languages.First());
            mocks.VerifyAll();
        }

        /// <summary>
        ///A test for RegisterCouple
        ///</summary>
        [TestMethod]
        public void RegisterCoupleTest()
        {
            var mocks = new MockRepository();
            ISession session = new DictionarySessionAdapter(new Dictionary<string, object>());
            var facade = mocks.StrictMock<IServiceFacade>();
            IHttpContextAdapter context = mocks.StrictMock<IHttpContextAdapter>();
            var target = new RegistrationModel(session, facade);
            target.Couple.ContactPerson = new ContactPerson()
                                              {
                                                  Email = "sdfsd@sdfsd.se",
                                                  FirstNames = "Pelle",
                                                  LastName = "Skog",
                                                  Telephone = "12123123"
                                              };
            target.Couple.CoupleId = null;
            target.Couple.Person1 = getPerson();
            target.Couple.Person2 = new Person(
                "Frank", "Sinatra", new PersonalNumber(new DateTime(1970, 01, 01), null), false, "Dansk", true, null);
            target.Ceremony = new Ceremony()
                                  {
                                      CeremonyLanguage = "Svenska",
                                      CertificateLanguage = "Svenska",
                                      SpecialNeeds = new string[] {"Rullstol"},
                                  };
            Expect.Call(facade.RegisterCouple(target.Couple, target.Ceremony)).Return(42);
            mocks.ReplayAll();
            target.RegisterCouple(null);
            Assert.AreEqual(42, target.RegistrationTicketId);
            mocks.VerifyAll();
        }

        /// <summary>
        ///A test for RegisterCouple. Reregistration
        ///</summary>
        [TestMethod]
        public void RegisterCoupleTest2()
        {
            var mocks = new MockRepository();
            ISession session = new DictionarySessionAdapter(new Dictionary<string, object>());
            var facade = mocks.StrictMock<IServiceFacade>();
            IHttpContextAdapter context = mocks.StrictMock<IHttpContextAdapter>();
            var target = new RegistrationModel(session, facade);
            target.Couple.ContactPerson = new ContactPerson()
                                              {
                                                  Email = "sdfsd@sdfsd.se",
                                                  FirstNames = "Svenne Gurra",
                                                  LastName = "Snorslev",
                                                  Telephone = "12123123"
                                              };
            target.Couple.CoupleId = null;
            target.Couple.Person1 = getPerson();
            target.Couple.Person2 = new Person(
                "Frank", "Sinatra", new PersonalNumber(new DateTime(1970, 01, 01), 1234), true, null, true, null);
            target.Ceremony = new Ceremony()
                                  {
                                      CeremonyLanguage = "Svenska",
                                      CertificateLanguage = "Engelska",
                                      SpecialNeeds = new string[0],
                                  };
            target.RegistrationTicketId = 52;
            Expect.Call(() => facade.UpdateCouple(null, target.Couple, target.Ceremony, 52));
            mocks.ReplayAll();
            target.RegisterCouple(null);
            Assert.AreEqual(52, target.RegistrationTicketId);
            mocks.VerifyAll();
        }

        private Person getPerson()
        {
            string surNames = "Svenne Gurra";
            string lastName = "Snorslev";
            var personId = new PersonalNumber("19700101", "0234");
            bool swedish = true;
            string nationality = string.Empty;
            bool sexMale = false;
            var postAddress = new Address
                                  {
                                      CoAddress = string.Empty,
                                      StreetAddress = "gatan 11",
                                      PostCode = "99999",
                                      City = "Sthlm",
                                      Country = "Sverige",
                                  };
            return new Person(surNames, lastName, personId, swedish, nationality, sexMale, postAddress);
        }
    }
}